<template>
  <div class="shopDialog">
    <el-dialog :title="isAdd ? '添加商品分类' : '修改商品分类'" :visible.sync="isShow" :before-close="cancel" center>
      <el-form ref="shopDialog" :model="form" :rules="rules">
        <el-form-item prop="pid" label="上级分类" :label-width="formLabelWidth">
          <el-select v-model="form.pid" placeholder="请选择活动区域">
            <el-option label="顶级分类" value="0"></el-option>
            <el-option v-for="item in shopList" :key="item.id" :label="item.catename" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="catename" label="分类名称" :label-width="formLabelWidth">
          <el-input v-model="form.catename" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item v-if="form.pid != 0" label="图片" :label-width="formLabelWidth">
          <!-- 图片上传  el-upload
            直接上传到云端服务器
            通过接口以文件流的格式传递给后端
        action 上传的地址   list-type文件列表的类型 -->
          <el-upload action="#" :auto-upload="false" :on-preview="onPreview" :on-remove="onRemove"
            list-type="picture-card" :on-change="onChange" :file-list="fileList" :on-exceed="handleExceed" :limit="1">
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch v-model="form.status" active-color="#13ce66" inactive-color="gray" :active-value="1"
            :inactive-value="2">
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { setCateadd, getOcateinfo, updataCateedit } from "../../request/api";
export default {
  data() {
    return {
      rules: {
        catename: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" },
        ],
        pid: [{ required: true, message: "请选择上级分类", trigger: "change" }],
      },
      value: 1,
      form: {
        pid: "",
        catename: "",
        img: "",
        status: 1,
      },
      formLabelWidth: "120px",
      dialogVisible: false, //控制图片预览的显示和隐藏
      dialogImageUrl: "", //图片地址
      fileList: [], //为空  则上传文件的组件也为空
    };
  },
  props: ["isShow", "isAdd"],
  computed: {
    ...mapGetters({
      shopList: "shop/getCate",
    }),
  },
  mounted() { },
  methods: {
    ...mapActions({
      getRoleAction: "shop/getCateAction",
    }),
    //点击取消按钮
    cancel() {
      this.reset();
      this.$emit("cancel");
    },
    //关闭dialog弹框后  还原默认值
    reset() {
      this.form = {
        pid: "",
        catename: "",
        img: "",
        status: 1,
      };
      //resetFields  对整个表单进行重置  将所有字段值重置为初始值并移除校验结果
      this.$refs.shopDialog.resetFields();
      //清空上传文件
      this.fileList = [];
    },
    //点击确定按钮
    confirm() {
      this.$refs.shopDialog.validate((val) => {
        if (val) {
          if (this.isAdd) {
            setCateadd(this.form)
              .then((res) => {
                if (res.code == 200) {
                  this.$message.success(res.msg);
                  //添加成功 关闭弹窗
                  this.cancel();
                  //添加数据后  重新获取列表
                  this.getRoleAction();
                }
              })
              .catch((err) => {
                this.$message.error(res.msg);
              });
          } else {
            updataCateedit(this.form)
              .then((res) => {
                if (res.code == 200) {
                  this.$message.success(res.msg);
                  //添加成功 关闭弹窗
                  this.cancel();
                  //添加数据后  重新获取列表
                  this.getRoleAction();
                }
              })
              .catch((err) => {
                this.$message.error(res.msg);
              });
          }
        }
      });
    },
    //数据回显
    lookup(id) {
      getOcateinfo({ id }).then((res) => {
        if (res.code == 200) {
          this.form = res.list;
          this.form.id = id;
          //数据回显的时候对图片进行单独配置
          this.fileList = this.form.img
            ? [{ url: "http://localhost:3000" + this.form.img }]
            : [];
        }
      });
    },
    //图片预览
    onPreview(file) {
      //   console.log(file, "预览文件");
      //显示弹窗
      this.dialogVisible = true;
      this.dialogImageUrl = file.url;
    },
    //图便移除
    onRemove(file, fileList) {
      //   console.log(file, "移除文件");
      //   console.log(fileList, "移除文件");
      //隐藏弹窗
      this.dialogVisible = false;
    },
    //图片改变时发生的变化
    onChange(file, fileList) {
      this.form.img = file.raw;
    },
    //超出文件上传的回调函数
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length
        } 个文件`
      );
    },
  },
};
</script>
<style scoped>

</style>